package cn.christ.com.mobilesafe.view.activity_sjfd;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;

import butterknife.BindView;
import butterknife.OnClick;
import cn.christ.com.mobilesafe.R;
import cn.christ.com.mobilesafe.common.Global;
import cn.christ.com.mobilesafe.constant.Constants;
import cn.christ.com.mobilesafe.receiver.MobileSafeAdminReceiver;

/**
 * Created by Administrator on 2018/7/22.
 */

public class SetingUpActivity4 extends BaseSetingUpActivity {
    @BindView(R.id.btn_activate)
    Button btnActivate;
    @BindView(R.id.btn_next)
    Button btnNext;
    @BindView(R.id.btn_pre)
    Button btnPre;
    private DevicePolicyManager dpm;
    private ComponentName who;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_setup4;
    }

    @Override
    public void initView() {

    }

    @OnClick({R.id.btn_activate, R.id.btn_next, R.id.btn_pre})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_activate:
                if (dpm.isAdminActive(who)) {
                    dpm.removeActiveAdmin(who);
                    btnActivate.setSelected(false);
                } else {
                    Intent intent = new Intent();
                    intent.setAction(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, who);
                    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "christ的手机卫士就是好");
                    startActivityForResult(intent, Constants.REQ_DEVICE_ADMIN);
                }

                break;
            case R.id.btn_next:
                if (!dpm.isAdminActive(who)) {
                    Global.showToast("请激活设备管理员权限");
                    return;
                }
                performNext();
                finish();
                overridePendingTransition(R.anim.next_enter, R.anim.next_exit);
                break;
            case R.id.btn_pre:
                performPre();
                finish();
                overridePendingTransition(R.anim.pre_enter, R.anim.pre_exit);
                break;
        }
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        who = new ComponentName(this, MobileSafeAdminReceiver.class);
    }

    @Override
    protected void performPre() {
        startActivityNotFinish(SetingUpActivity3.class);
    }

    @Override
    protected void next() {
        if (!dpm.isAdminActive(who)) {
            Global.showToast("请激活设备管理员权限");
            return;
        }
        performNext();
        finish();
        overridePendingTransition(R.anim.next_enter, R.anim.next_exit);
    }

    @Override
    protected void performNext() {
        startActivityNotFinish(SetingUpActivity5.class);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.REQ_DEVICE_ADMIN) {
            btnActivate.setSelected(dpm.isAdminActive(who));
        }
    }
}
